<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>栈_应用_十进制转二进制</title>
</head>

<body>
  <script src="../../../dist/190719-utils.js"></script>
  <script>
    /* 
    使用Stack封装一个功能函数: 十进制(decimal)转二进制(binary)
    */
    function dec2bin(decNum) {
      //创建一个用于保存2进制的stack
      const stack = new aUtils.Stack()
      while (decNum > 0) {
        const remainder = decNum % 2
        stack.push(remainder)
        decNum = Math.floor(decNum / 2)
      }
      let result = '' //拼接2进制
      while (!stack.isEmpty()) {
        result += stack.pop()
      }
      return result
    }
    // 测试
    console.log(dec2bin(10))
    console.log(dec2bin(233))
  </script>
</body>

</html>